package com.common.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.common.util.PropertiesUtils;

public class DBSource {
	private static Logger log = Logger.getLogger(DBSource.class);

	public static Connection getConnection() { 
        
			Connection conn = getConnByProxoolDS();
			
            return conn;        
        
    }


	private static Connection getConnByProxoolDS() { 
		try{
            Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");//proxool驱动类        
            Connection conn = DriverManager.getConnection("proxool.mypool"); 
            return conn;        
        }catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("连接池连接失败: " + e.getMessage());
        }
    }
	
	private static Connection getConnByTomcatDS() { 
        try{
        	InitialContext ctx = new InitialContext();
			Context envCtx = (Context) ctx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/mypool");
			Connection conn = ds.getConnection();
			
            return conn;        
        }catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("连接池连接失败: " + e.getMessage());
        }
    }
	private static Connection getConnByJbossDS() { 
        try{
        	InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:jdbc/mypool");
			Connection conn = ds.getConnection();
			
            return conn;        
        }catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("连接池连接失败: " + e.getMessage());
        }
    }
	private static Connection getConnByJdbc() {
		try {
			Connection conn = null;
			PropertiesUtils pro = new PropertiesUtils("/jdbc.properties");
			String driver = (String) pro.getResouceString("driver");
			String url = (String) pro.getResouceString("url");
			String username = (String) pro.getResouceString("username");
			String password = (String) pro.getResouceString("password");
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url, username, password);
			return conn;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException("连接池连接失败: " + e.getMessage());
		}
	}

	
	
	
	public static void main(String[] args) {

		try {
			System.out.println("conn=" + DBSource.getConnection());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
